<template>
  <!-- <view class="status_bar"></view> -->
  <view class="page-login">
    <view class="page-header">
      <view class="left-title">{{ $t('account.login.sub_title') }}</view>
      <view class="right-btn" @click="proxy.$util.back()"><uni-icons type="closeempty" size="28"></uni-icons></view>
    </view>
    <uni-forms :modelValue="data.form" :rules="data.rules" ref="form" class="form" label-position="top">
      <uni-forms-item :label="$t('account.login.input_email')" name="email">
        <uni-easyinput
          placeholderStyle="font-size:14px"
          type="text"
          v-model="data.form.email"
          :styles="{borderColor: '#ccc'}"
          :placeholder="$t('account.login.input_account')"
          :clearable="false"
        />
      </uni-forms-item>
      <uni-forms-item :label="$t('account.login.t_password')" name="password">
        <uni-easyinput
          placeholderStyle="font-size:14px"
          type="password"
          :styles="{borderColor: '#ccc'}"
          v-model="data.form.password"
          :placeholder="$t('account.login.input_password')"
        />
      </uni-forms-item>
    </uni-forms>
    <view class="bottom-text">
      <!-- <text class="forgot-password">{{ $t('account.login.t_forgot') }}</text> -->
      <text></text>
      <view class="register-link" @click="proxy.$util.navigateTo('/pages/account/register/register')">
        <text>{{ $t('account.login.register_explain') }}</text>
        <text class="register-text">{{ $t('account.login.t_register') }}</text>
      </view>
    </view>
    <button @click="submit" class="btn-primary btn-login">{{ $t('account.login.t_login') }}</button>

    <view class="privacy-policy">{{ $t('account.login.privacy_policy_text') }} <text class="link" @click="proxy.$util.navigateTo('/pages/webview/index', {src: 'pages/12'})">{{ $t('account.login.privacy_policy') }}</text></view>
  </view>
</template>

<script setup>
import { t } from '@/locale/index'
const { proxy } = getCurrentInstance()

const form = ref()

const data = reactive({
  form: {
    email: '',
    password: '',
  },

  rules: {
    email: {
      rules: [
        { required: true, errorMessage: t('account.login.input_account'), trigger: 'blur' },
        { format: 'email', errorMessage: t('common.input_email_error'), trigger: 'blur' },
      ],
    },
    password: {
      rules: [{ required: true, errorMessage: t('account.login.input_password'), trigger: 'blur' }],
    },
  },
})

const submit = () => {
  form.value.validate().then(res => {
    proxy.$http.post('login', data.form).then(res => {
      uni.setStorageSync('account', res)
      uni.setStorageSync('access-token', res.access_token)
      proxy.$util.msg({ title: t('account.login.input_success'), icon: 'success' }, () => {
        if (uni.getStorageSync('login_after_url')) {
          proxy.$util.backTo(uni.getStorageSync('login_after_url'))
          return
        }
        uni.switchTab({ url: '/pages/account/home/home' })
      })
    })
  })
}

// 页面卸载
onUnload(() => {
  uni.removeStorageSync('login_after_url')
})
</script>

<style lang="scss" scoped>
@import './login.scss';
</style>
